package com.telecom.weixi;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.telecom.weixi.net.RespListener;
import com.telecom.weixi.net.RespResult;
import com.telecom.weixi.net.WebServiceEngine;
import com.telecom.weixi.parse.ServBaseInfo2;
import com.telecom.weixi.parse.ServBaseParse2;
import com.telecom.weixi.parse.XmlParse;
import com.telecom.weixi.util.Utils;
import com.telecom.weixi.view.TitleView;

public class InputSearchActivity extends BaseActivity implements RespListener {
	private TitleView title;
	private EditText input_content_1;
	private EditText input_content_2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.input_search);
		initView();

		int type = getIntent().getIntExtra(TopMenuActivity.TYPE_TAG, TopMenuActivity.TYPE_USER_SEARCH);
		String strTitle;
		switch (type) {
		case TopMenuActivity.TYPE_USER_SEARCH:
			strTitle = getString(R.string.search_user);
			break;
		case TopMenuActivity.TYPE_CUST_SEARCH:
			strTitle = getString(R.string.search_cust);
			break;
		case TopMenuActivity.TYPE_TERM_SEARCH:
			strTitle = getString(R.string.search_term);
			break;
		case TopMenuActivity.TYPE_OWE_SEARCH:
			strTitle = getString(R.string.search_owe);
			break;
		default:
			strTitle = getString(R.string.search_user);
		}
		title.setTitle(strTitle);

		initWaitDialog();
	}

	private void initView() {
		title = (TitleView) findViewById(R.id.title_view);
		title.setPreButtonListener(backListener);

		findViewById(R.id.choose1).setOnClickListener(searchListener);
		findViewById(R.id.choose2).setOnClickListener(searchListener);
		input_content_1 = (EditText) findViewById(R.id.input_content1);
		input_content_2 = (EditText) findViewById(R.id.input_content2);

	}

	private void hideSI() {
		((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(InputSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}

	ProgressDialog waitDialog;

	public void initWaitDialog() {
		if (waitDialog == null) {
			waitDialog = new ProgressDialog(this);
			waitDialog.setMessage(getResources().getString(R.string.search_and_wait));
			waitDialog.setCancelable(true);
		}
	}

	public void showWait() {
		waitDialog.show();
	}

	public void stopWait() {
		if (waitDialog != null && waitDialog.isShowing()) {
			waitDialog.hide();
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		if (waitDialog != null) {
			waitDialog.dismiss();
		}
		super.onStop();
	}

	View.OnClickListener backListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			InputSearchActivity.this.finish();
		}
	};

	View.OnClickListener searchListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			hideSI();
			String txtSearch = "";
			boolean isByName;
			switch (v.getId()) {
			case R.id.choose1:
				txtSearch = input_content_1.getText().toString();
				isByName = false;
				break;
			case R.id.choose2:
				txtSearch = input_content_2.getText().toString();
				isByName = true;
				break;
			default:
				return;
			}
			if (txtSearch.length() == 0) {
				Utils.showErrorDialog(InputSearchActivity.this, InputSearchActivity.this.getString(R.string.warning_search), InputSearchActivity.this.getString(R.string.promp_confirm), null);
				return;
			}

			showWait();
			WebServiceEngine wse = new WebServiceEngine();
			wse.setRespListener(InputSearchActivity.this);
			if (isByName) {
				wse.getUserNameSearch(WeixiApplication.getApplication().getUserId(), txtSearch, WeixiApplication.getApplication().getLatnId(), 10, 1);
			} else {
				wse.getAccNbrSearchAsync(WeixiApplication.getApplication().getUserId(), txtSearch, WeixiApplication.getApplication().getLatnId(), 10, 1);
			}
		}
	};

	void Debug(String info) {
		Utils.debug("test", "[InputSearcjActivity] " + info);
	}

	@Override
	public void OnDataRecv(RespResult resp) {
		// TODO Auto-generated method stub
		stopWait();
		ServBaseParse2 parseHelper = new ServBaseParse2();
		XmlParse.parse(Utils.String2InputStream(resp.toString()), parseHelper);

		ArrayList<ServBaseInfo2> infos = parseHelper.getData();
		if (infos != null && infos.size() > 0) {
			Intent intent = new Intent(InputSearchActivity.this, SearchBoardActivity.class);
			Bundle data = new Bundle();
			data.putParcelableArrayList(SearchBoardActivity.Tag_Search_Info, infos);
			intent.putExtras(data);
			startActivityForResult(intent, WeixiApplication.CODE_REQ);
		} else {
			Utils.showErrorDialog(this, getString(R.string.error_search), getString(R.string.promp_confirm), null);
		}
	}

}
